package com.uit.httt04.duongvanlam.facedetection.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

public class ParserXML extends Object
{
	public static TrainingXML parse(InputStream inputStream) throws IOException, SAXException
	{
		final TrainingXML trainingXML = new TrainingXML();

		final List<AlgorithmXML> listAlgorithm = new ArrayList<AlgorithmXML>();
		final List<ObjectXML> listObject = new ArrayList<ObjectXML>();
		final List<ProjectedObjectXML> listProjectedObject = new ArrayList<ProjectedObjectXML>();
		final List<IDToNameXML> listIDToNameXML = new ArrayList<IDToNameXML>();
		final List<String> listAllowedExt = new ArrayList<String>();

		final AlgorithmXML curAlgorithm = new AlgorithmXML();
		final MatrixXML curEigenvectorMatrix = new MatrixXML();
		final MatrixXML curEigenvalueMatrix = new MatrixXML();
		final MatrixXML curProjectedObjectMatrix = new MatrixXML();
		final ObjectXML curObject = new ObjectXML();
		final ProjectedObjectXML curProjectedObject = new ProjectedObjectXML();
		final IDToNameXML curIDToNameXML = new IDToNameXML();

		// Training
		RootElement rootElement = new RootElement(TAGXML.Training);
		rootElement.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				trainingXML.setListAlgorithm(listAlgorithm);
			}
		});
		// Algorithm
		Element algorithm = rootElement.getChild(TAGXML.Algorithm);
		algorithm.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				listAlgorithm.add(curAlgorithm.Copy());
			}
		});
		// Name
		algorithm.getChild(TAGXML.Name).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setAlgorithmName(body);
			}
		});
		// TimestampStarting
		algorithm.getChild(TAGXML.TimestampStarting).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setTimestampStarting(Long.valueOf(body));
			}
		});
		// TrainingLengthByMilliseconds
		algorithm.getChild(TAGXML.TrainingLengthByMilliseconds).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setTrainingLengthByMilliseconds(Long.valueOf(body));
			}
		});
		// TrainingFolderPath
		algorithm.getChild(TAGXML.TrainingFolderPath).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setTrainingFolderPath(body);
			}
		});
		// ImageHeight
		algorithm.getChild(TAGXML.ImageHeight).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setImageHeight(Integer.valueOf(body));
			}
		});
		// ImageWidth
		algorithm.getChild(TAGXML.ImageWidth).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setImageWidth(Integer.valueOf(body));
			}
		});
		// TotalAmountOfImages
		algorithm.getChild(TAGXML.TotalAmountOfImages).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setTotalAmountOfImages(Integer.valueOf(body));
			}
		});
		// TotalAmountOfFaces
		algorithm.getChild(TAGXML.TotalAmountOfFaces).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setTotalAmountOfFaces(Integer.valueOf(body));
			}
		});
		// Objects
		Element Objects = algorithm.getChild(TAGXML.Objects);
		Objects.setStartElementListener(new StartElementListener()
		{

			@Override
			public void start(Attributes arg0)
			{
				listObject.clear();
			}
		});
		Objects.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setObjects(listObject);
			}
		});
		// Object
		Element Object = Objects.getChild(TAGXML.Object);
		Object.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				listObject.add(curObject.Copy());
			}
		});
		// PersonName
		Object.getChild(TAGXML.PersonName).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curObject.setPersonName(body);
			}
		});
		// ImagePath
		Object.getChild(TAGXML.ImagePath).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curObject.setImagePath(body);
			}
		});
		// EigenvectorMaxN
		algorithm.getChild(TAGXML.EigenvectorMaxN).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setEigenvectorMaxN(Integer.valueOf(body));
			}
		});
		// MatrixDataSplitBy
		algorithm.getChild(TAGXML.MatrixDataSplitBy).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curAlgorithm.setMatrixDataSplitBy(body.charAt(0));
			}
		});
		// EigenvectorMatrix
		Element EigenvectorMatrix = algorithm.getChild(TAGXML.EigenvectorMatrix);
		EigenvectorMatrix.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setEigenvectorMatrix(curEigenvectorMatrix.Copy());
			}
		});
		// MatrixHeight
		EigenvectorMatrix.getChild(TAGXML.MatrixHeight).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvectorMatrix.setHeight(Integer.valueOf(body));
			}
		});
		// MatrixWidth
		EigenvectorMatrix.getChild(TAGXML.MatrixWidth).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvectorMatrix.setWidth(Integer.valueOf(body));
			}
		});
		// MatrixData
		EigenvectorMatrix.getChild(TAGXML.MatrixData).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvectorMatrix.setData(body);
			}
		});

		// EigenvectorMatrix
		Element EigenvalueMatrix = algorithm.getChild(TAGXML.EigenvalueMatrix);
		EigenvalueMatrix.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setEigenvalueMatrix(curEigenvalueMatrix.Copy());
			}
		});
		// MatrixHeight
		EigenvalueMatrix.getChild(TAGXML.MatrixHeight).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvalueMatrix.setHeight(Integer.valueOf(body));
			}
		});
		// MatrixWidth
		EigenvalueMatrix.getChild(TAGXML.MatrixWidth).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvalueMatrix.setWidth(Integer.valueOf(body));
			}
		});
		// MatrixData
		EigenvalueMatrix.getChild(TAGXML.MatrixData).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curEigenvalueMatrix.setData(body);
			}
		});

		// ProjectedObjects
		Element ProjectedObjects = algorithm.getChild(TAGXML.ProjectedObjects);
		ProjectedObjects.setStartElementListener(new StartElementListener()
		{

			@Override
			public void start(Attributes attributes)
			{
				listProjectedObject.clear();
			}
		});
		ProjectedObjects.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setProjectedObjects(listProjectedObject);
			}
		});
		// ProjectedObject
		Element ProjectedObject = ProjectedObjects.getChild(TAGXML.ProjectedObject);
		ProjectedObject.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				listProjectedObject.add(curProjectedObject.Copy());
			}
		});
		// PersonName
		ProjectedObject.getChild(TAGXML.PersonName).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curProjectedObject.setPersonName(body);
			}
		});
		// ImagePath
		ProjectedObject.getChild(TAGXML.ImagePath).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curProjectedObject.setImagePath(body);
			}
		});
		// ProjectedObjectMatrix
		Element ProjectedObjectMatrix = ProjectedObject.getChild(TAGXML.ProjectedObjectMatrix);
		ProjectedObjectMatrix.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curProjectedObject.setMatrix(curProjectedObjectMatrix.Copy());
			}
		});
		// MatrixHeight
		ProjectedObjectMatrix.getChild(TAGXML.MatrixHeight).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curProjectedObjectMatrix.setHeight(Integer.valueOf(body));
			}
		});
		// MatrixWidth
		ProjectedObjectMatrix.getChild(TAGXML.MatrixWidth).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curProjectedObjectMatrix.setWidth(Integer.valueOf(body));
			}
		});
		// MatrixData
		ProjectedObjectMatrix.getChild(TAGXML.MatrixData).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curProjectedObjectMatrix.setData(body);
			}
		});
		// AutoID
		Element autoID = algorithm.getChild(TAGXML.AutoID);
		autoID.setStartElementListener(new StartElementListener()
		{

			@Override
			public void start(Attributes arg0)
			{
				listIDToNameXML.clear();
			}
		});
		autoID.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setAutoID(listIDToNameXML);
			}
		});
		// IDToName
		Element iDToName = autoID.getChild(TAGXML.IDToName);
		iDToName.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				listIDToNameXML.add(curIDToNameXML.Copy());
			}
		});
		// ID
		iDToName.getChild(TAGXML.ID).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curIDToNameXML.setID(Integer.valueOf(body));
			}
		});
		// Name
		iDToName.getChild(TAGXML.Name).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				curIDToNameXML.setName(body);
			}
		});
		// AllowedExt
		Element allowedExt = algorithm.getChild(TAGXML.AllowedExt);
		allowedExt.setStartElementListener(new StartElementListener()
		{

			@Override
			public void start(Attributes arg0)
			{
				listAllowedExt.clear();
			}
		});
		allowedExt.setEndElementListener(new EndElementListener()
		{

			@Override
			public void end()
			{
				curAlgorithm.setAllowedExt(listAllowedExt);
			}
		});
		// Ext
		allowedExt.getChild(TAGXML.Ext).setEndTextElementListener(new EndTextElementListener()
		{

			@Override
			public void end(String body)
			{
				listAllowedExt.add(body);
			}
		});

		Xml.parse(inputStream, Xml.Encoding.UTF_8, rootElement.getContentHandler());
		return trainingXML;
	}
}
